/*
 * Copyright (c) 2003-2010, Live Media Pty. Ltd.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted
 * provided that the following conditions are met:
 *
 *  1. Redistributions of source code must retain the above copyright notice, this list of
 *     conditions and the following disclaimer.
 *  2. Redistributions in binary form must reproduce the above copyright notice, this list of
 *     conditions and the following disclaimer in the documentation and/or other materials
 *     provided with the distribution.
 *  3. Neither the name of Live Media nor the names of its contributors may be used to endorse
 *     or promote products derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.colony;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;

import com.argot.TypeException;
import com.argot.TypeLibrary;
import com.argot.remote.MetaLocation;
import com.argot.remote.MetaObject;
import com.colony.remote.RemoteException;

public class ColonyEnvironment
{
	private TypeLibrary _library;
	private IContainer _container;
	
	private HashMap _clients;
	private HashMap _hosts;
	
	public ColonyEnvironment( IContainer container, TypeLibrary library )
	{
		System.out.println("Colony Version 1.3.b");
		System.out.println("Copyright 2004-2010 (C) Live Media Pty Ltd.");
		System.out.println("www.einet.com.au\n");
		
		_library = library;
		_container = container;
		_clients = new HashMap();
		_hosts = new HashMap();
	}
	
	public IContainer getHostContainer()
	{
		return _container;
	}

    public TypeLibrary getTypeLibrary()
    {
    	return _library;
    }
    
	public Object getFront( MetaLocation location ) 
	throws ColonyException, RemoteException, TypeException
	{
		if (location.getHost() == null )
		{
			return _container.getObject(location);
		}

		IColonyClient client = getClient( location.getHost() );
		if ( client == null )
		{
			throw new RemoteException("host connection to host available");
		}
		
		return client.getFront( location );
	}
	
	public Object getFront( MetaObject object )
	throws ColonyException, RemoteException, TypeException
	{
		MetaLocation location = object.getLocation();
		if (location.getHost() == null )
		{
			return _container.getObject(location);
		}

		IColonyClient client = getClient( location.getHost() );
		if ( client == null )
		{
			throw new RemoteException("host connection not available");
		}
		
		return client.getFront( object );
		
	}
	
	public void addClient( String hostName, IColonyClient client ) 
	throws ColonyException, RemoteException, TypeException, IOException
	{
		client.bind( hostName, this );
		_clients.put( hostName, client );
	}
	
	public IColonyClient getClient( String hostName )
	{
		return (IColonyClient) _clients.get( hostName );
	}
	
	public Iterator getClientNames()
	{
		return _clients.keySet().iterator();
	}
	
	public void addServer( String hostName, IColonyServer server ) 
	throws ColonyException, RemoteException, TypeException
	{
		server.bind( this );
		_hosts.put( hostName, server );
	}
	
	public Iterator getServerHostNames()
	{
		return _hosts.keySet().iterator();
	}

	public void addObject( MetaLocation location, String typeName, Object object )
	throws ColonyException
	{
		_container.addObject(location, typeName, object);
	}

	public void disconnect(String hostName)
	throws ColonyException
	{
		IColonyClient client = getClient(hostName);
		client.disconnect();
		_clients.remove(hostName);
		
	}
}
